<?php

abstract class Common_DataTable_Model_Abstract 
    extends    ClassStructure_Mixin_Delegator 
    implements Common_DataTable_Model 
{
    
    private $arr_summaries = array();
    
    public function __construct() {
        parent::__construct();
    }
    
    public function addDataSummary(Common_DataTable_Summary $obj_summary) {
        $this->arr_summaries[] = $obj_summary;
    }
    
    public function clearDataSummaries() {
        $this->arr_summaries = array();
    }
    
    public function getDataSummaries() {
        return $this->arr_summaries;
    }
    
    public function hasDataSummaries() {
        return !empty($this->arr_summaries);
    }
    
    public function hasFeature($enm_feature) {
        return false;
    }
    
    public function getDataModel($arr_headers, $int_size = null, $int_offset = null) {
        
        $arr_data = $this->getData($int_size, $int_offset);
        
        $int_data_count = 0;
        if(isset($arr_data['count'])) {
            $int_data_count = $arr_data['count'];
        }
        
        $mix_grouping = null;
        if(true) {
            $mix_grouping = $this->getGroupDefinition();
        }
        
        $arr_footers = array();
        
        if($this->hasDataSummaries()) {
            
            $arr_summaries = $this->getDataSummaries();
            
            foreach($arr_summaries as $obj_summary) {
                $obj_summary->initialise(end($arr_headers), array_keys($arr_data['groups']));
            }
            
            foreach($arr_data['rows'] as $str_group_id => $arr_data_group) {
                $arr_data_copy = array();
                foreach($arr_data_group as $str_row_key => $arr_row) {
                    foreach($arr_summaries as $obj_summary) {
                        $obj_summary->reviewRow($arr_row, $mix_grouping);
                    }
                    $arr_data_copy[$str_row_key] = $arr_row;
                }
                
                $arr_data['rows'][$str_group_id] = $arr_data_copy;
            }
            
            
            
            foreach($arr_summaries as $obj_summary) {
                
                $mix = $obj_summary->getData($int_data_count);
                if(is_array($mix)) {
                    $arr_footers[] = $mix;
                }
                
                if(!is_null($mix_grouping)) {
                    
                    $str_type = $obj_summary->getPositionType();
                    
                    foreach($arr_data['groups'] as $str_grouping_index => $arr_group_meta) {
                        
                        $str_group_label = $arr_group_meta['label'];
                        $int_group_count = $arr_group_meta['count'];
                        
                        $mix = $obj_summary->getDataForGroup($str_grouping_index, $str_group_label, $int_group_count);
                        if(is_array($mix)) {
                            $arr_data['groups'][$str_grouping_index][$str_type][] = $mix;
                        }
                    }
                        
                }
                
            }
        }
        
        $arr_data['footers'] = $arr_footers;
        
        return $arr_data;
        
    }
    
    public function getData($int_size = null, $int_offset = null) {
        return array(
            'count' => 0,
            'groups' => array(),
            'rows' => array()
        );
    }
    
    public function toArray()
    {
        return array();
    }
    
}
